<?php

declare(strict_types=1);
/**
 * This file is part of the youyao/admin-api.
 * (c) youyao <info@nuancebiotech.cn>
 * This source file is subject to the license under the project that is bundled.
 */

namespace App\Helper;

use App\Common\Upload;
use App\Http\Logic\Admin\UploadLogic;
use App\Http\Logic\PC\UsersLogic;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;

class Helper
{
    /**
     * @param $mobile
     * @return bool
     */
    public static function mobileMatch($mobile): bool
    {
        if (!preg_match("/^1[3456789]\d{9}$/", $mobile)) {
            return false;
        }
        return true;
    }


    /**
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public static function adminInfo()
    {
        return Auth::guard('admin')->user();
    }

    /**
     * @return mixed
     */
    public static function adminById()
    {
        return Auth::guard('admin')->user()->id;
    }


    /**
     * @return mixed
     */
    public static function pcByMobile()
    {
        $pcInfo  = JWTAuth::parseToken()->getPayload();
        $decInfo = json_decode(json_encode($pcInfo), true);
        $uInfo   = json_decode($decInfo['uid'], true);
        if(isset($uInfo['user_id'])) {
            $userInfo = UsersLogic::getUserInfo($uInfo['user_id']);
            if(!$userInfo) {
                return false;
            }
            return $userInfo->mobile;
        }
        return false;
    }

    public static function createRes($path)
    {
        return UploadLogic::createRes($path);
    }

    /**
     * @param $url
     * @return string
     */
    public static function buildQiNiuUrl($url)
    {
        $auth              = new \Qiniu\Auth(env('QINIU_ACCESS_KEY'), env('QINIU_SECRET_KEY'));
        $expiry            = 172800; // 有效期，单位：秒 2天
        return $auth->privateDownloadUrl($url, $expiry);
    }

    public static function timediff($begin_time,$end_time)
    {
        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }

        //计算天数
        $timediff = $endtime-$starttime;
        $days = intval($timediff/86400);
        //计算小时数
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        //计算分钟数
        $remain = $remain%3600;
        $mins = intval($remain/60);
        //计算秒数
        $secs = $remain%60;
        $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
        return $res;
    }
}
